package net.amsoft.iservice.management.util;

import net.amsoft.iservice.management.util.IBrokerConstants;

import org.apache.log4j.Logger;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class ManagerHibernateUtil {
    private static Logger logger = Logger.getLogger(ManagerHibernateUtil.class);
	private static final SessionFactory sessionFactoryISSO;
	private static final SessionFactory sessionFactoryILink;
	private static final SessionFactory sessionFactoryIContact;

	static {
	    try {
			sessionFactoryILink = new Configuration().configure(
					"ilink/hibernate.cfg.xml").buildSessionFactory();
		} catch (Throwable ex) {
			System.err.println("Initial SessionFactory creation for ILinkfailed."
							+ ex);
			logger.error("static(): Exception while calling buildSessionFactory() for ILink");
			throw new ExceptionInInitializerError(ex);
		}
		try {
			sessionFactoryISSO = new Configuration().configure(
					"isso/hibernate.cfg.xml").buildSessionFactory();
		} catch (Throwable ex) {
			System.err.println("Initial SessionFactory creation for ISSO failed."
							+ ex);
			logger.error("static(): Exception while calling buildSessionFactory() for ISSO");
			throw new ExceptionInInitializerError(ex);
		}
		
		try {
			sessionFactoryIContact = new Configuration().configure(
					"icontact/hibernate.cfg.xml").buildSessionFactory();
		} catch (Throwable ex) {
			System.err.println("Initial SessionFactory creation for ICOntact failed."
							+ ex);
			logger.error("static(): Exception while calling buildSessionFactory() for IContact");
			throw new ExceptionInInitializerError(ex);
		}

	}

	public static SessionFactory getSessionFactory(String sServiceType) {
		if ((sServiceType.equals(IBrokerConstants.getProperty(IBrokerConstants.SERVICE_ISSO))))
			return sessionFactoryISSO;
		else if ((sServiceType.equals(IBrokerConstants.getProperty(IBrokerConstants.SERVICE_ILINK))))
			return sessionFactoryILink;
		else if ((sServiceType.equals(IBrokerConstants.getProperty(IBrokerConstants.SERVICE_ICONTACT))))
			return sessionFactoryIContact;
		return null;
	}

}